//
//  PainterCrosshairsAVG.m
//  p4
//
//  Created by CaoJun on 15/6/1.
//  Copyright (c) 2015年 com.baidao. All rights reserved.
//

#import "PainterCrosshairsAVG.h"

using namespace ytx;

@implementation PainterCrosshairsAVG

- (void)drawCrosshairsYText:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ViewModelIndexPosition *) indexPosition withPx:(CGFloat) px withValue:(double) value withText:(NSString *) text
{
    [super drawCrosshairsYText:cgrect withContext:ctx withViewModelIndexPosition:indexPosition withPx:px withValue:value withText:text];
    
    AVGData * data = (AVGData *) indexPosition->getData();
    
    double prevValue = data->prevValue;
    
    YTXConfig * config = YTXConfig::getInstance();
    ytx::Style style = config->style;
    Rectangle & rect = indexPosition->getRectangle();
    
    NSString * font = [NSString stringWithUTF8String:style.crosshairsTextFont.c_str()];
    
    NSString * rightText = [NSString stringWithUTF8String:Tools::numberToFixedPercentString(fabs((value * 100 / prevValue) - 100)).c_str()];
    
    CGRect backgroundRect = [ctx getTextRect:rightText withFont:font withSize:style.crosshairsTextSize];
    backgroundRect.size.width+=2;
    backgroundRect.size.height+=2;
    backgroundRect.origin.x = rect.getEndX()-backgroundRect.size.width;
    backgroundRect.origin.y = px-backgroundRect.size.height*0.5;
    
    [ctx fillRect:backgroundRect withFillColor:[NSString stringWithUTF8String:style.crosshairsTextBackgorundColor.c_str()]];
    
    [ctx fillText:rightText withX:rect.getEndX() withY:px withColor:[NSString stringWithUTF8String:style.crosshairsTextColor.c_str()] withFont:font withSize:style.crosshairsTextSize withAnchor:RightMiddle];
    
}

- (void)drawCrosshairsXText:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ytx::ViewModelIndexPosition *) indexPosition withPx:(CGFloat) px  withIndex:(NSInteger) index
{
    YTXConfig * config = YTXConfig::getInstance();
    ytx::Style style = config->style;
    Rectangle & rect = indexPosition->getRectangle();
    
    NSString * font = [NSString stringWithUTF8String:style.crosshairsTextFont.c_str()];
    
    AVGData * data = (AVGData *)indexPosition->getData();
    //TODO 通过index取时间，按理说应该和通过startIndex 取出来一致才对
    NSString * text = [NSString stringWithUTF8String: data->getTimeDurationByIndex(index).toStringHHmm().c_str()];
    
    CGRect backgroundRect = [ctx getTextRect:text withFont:font withSize:style.crosshairsTextSize];
    backgroundRect.size.width+=2;
    backgroundRect.size.height+=2;
    backgroundRect.origin.x = px - backgroundRect.size.width*0.5;
    backgroundRect.origin.y = rect.getEndY();
    
    [ctx fillRect:backgroundRect withFillColor:[NSString stringWithUTF8String:style.crosshairsTextBackgorundColor.c_str()]];
    
    [ctx fillText:text withX:px withY:rect.getEndY() withColor:[NSString stringWithUTF8String:style.crosshairsTextColor.c_str()] withFont:font withSize:style.crosshairsTextSize withAnchor:CenterTop];
}

@end
